package com.wustl.android.hood;


import com.google.android.maps.MapActivity;


import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.Menu.Item;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;  	 
import android.widget.AdapterView.OnItemSelectedListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.ViewGroup.LayoutParams;


public class HoodMain extends MapActivity {
    /** Called when the activity is first created. */
	
	private static final int ACTIVITY_CREATE=0;
    //private static final int ACTIVITY_EDIT=1;
    
	private static final int INIT_DB_ID = Menu.FIRST;
	//private static final int FACEBOOK_ID = Menu.FIRST;
	//private static final int MAP_ID = FACEBOOK_ID + 1;
	//private static final int TWITTER_ID = MAP_ID + 1;

	protected LocationAdapter	mAdapter;
	protected DbLocations mDbHelper;
	protected DbActions mDbActions;
	
	@Override
	public void onCreate(Bundle icicle) {
		super.onCreate(icicle);
		setContentView(R.layout.main);
		
		ListView lvLocations = (ListView)findViewById(R.id.location_list);
		lvLocations.setEmptyView(findViewById(R.id.empty_location));
		lvLocations.setOnItemClickListener( new OnItemClickListener() {
			public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3) {
				// @TODO: Need optimization
				Cursor rowIDs = mDbHelper.fetchAllLocationsID();
				if( rowIDs.count() > arg2 ) {
					rowIDs.moveTo(arg2);
					
					Intent i = new Intent(HoodMain.this, ItemMain.class);
					i.setData(Uri.parse(""+rowIDs.getLong(0)));
		        	startSubActivity(i, ACTIVITY_CREATE);	
				}
			}
		}	);
		
		mDbHelper = new DbLocations(this);
		mDbActions = new DbActions(this);
		
		mDbHelper.open();
		mDbActions.open();
		
		mAdapter = new LocationAdapter(this, mDbHelper);
		lvLocations.setAdapter( mAdapter );
	}
	
	 public boolean onCreateOptionsMenu(Menu menu) {
        super.onCreateOptionsMenu(menu);
        menu.add(0, INIT_DB_ID, "Init DB");
        //menu.add(0, FACEBOOK_ID, R.string.menu_facebook);
        //menu.add(0, MAP_ID, R.string.menu_map);
        //menu.add(0, TWITTER_ID, R.string.menu_twitter);
        return true;
    }

    @Override
    public boolean onMenuItemSelected(int featureId, Item item) {
    	//Intent i = null;
        switch(item.getId()) {
        case INIT_DB_ID:
        	DbLocations db = new DbLocations(this);
        	db.init();
        	
        	db.createLocation("Washington University", 38.647059, -90.305707, 0, 8, DbLocations.convertMapType( MAP_TYPE.MT_MAP ));
			db.createLocation("Forest Park", 38.646439, -90.280066, 1, 8, DbLocations.convertMapType( MAP_TYPE.MT_SATELLITE ));
			db.createLocation("Saint Louis Arch", 38.62485, -90.186596, 0, 9, DbLocations.convertMapType( MAP_TYPE.MT_SATELLITE ));
			db.createLocation("the White House", 38.897661, -77.036564, 2, 8, DbLocations.convertMapType( MAP_TYPE.MT_SATELLITE ));
			
        	break;
//        case FACEBOOK_ID:
//        	i = new Intent(this, FaceBookMain.class);
//        	break;
//        case MAP_ID:
//        	i = new Intent(this, MapMain.class);
//        	break;
//        case TWITTER_ID:
//        	i = new Intent(this, TwitterMain.class);
//        	break;
        }
//        if( i != null ) {
//        	startSubActivity(i, ACTIVITY_CREATE);
//        	return true;
//        }
       
        return super.onMenuItemSelected(featureId, item);
    }
    
    public static int getTypeIconResource(int type) {
		switch(type) {
		case 0:					return R.drawable.map;
		case 1:					return R.drawable.images;
		default:				return R.drawable.help;
		}
	}

    public class LocationAdapter extends BaseAdapter {
    	private Context mContext;
    	 
    	private DbLocations mDbHelper;
    	private Cursor mRowIDs;
    	public LocationAdapter(Context c, DbLocations db) {
    		mContext = c;
    		mDbHelper = db;
    		
    		mRowIDs = mDbHelper.fetchAllLocationsID();
    	}
		public int getCount() {
			if( mRowIDs == null )
				return 0;
			else
				return  mRowIDs.count();
		}

		public Object getItem(int position) { return position; }
		public long getItemId(int position) { return position; }
		
		public View getView(int position, View convertView, ViewGroup parent) {
            if( mRowIDs.moveTo(position) == false )
            	return null;
           
            // Retrieve one location
            // @TODO: We might need to optimize this part
			Cursor location = mDbHelper.fetchLocation(mRowIDs.getLong(0));
            
			LinearLayout ll = new LinearLayout(mContext);
			{
				ImageView i = new ImageView(mContext);

				final int type = location.getInt( location.getColumnIndex(DbLocations.KEY_TYPE) );
				i.setImageResource( getTypeIconResource(type) );

	            i.setAdjustViewBounds(true);
	            
	            i.setPadding(i.getPaddingLeft(), 1, 5, i.getPaddingBottom());
	            ll.addView(i, new LinearLayout.LayoutParams(20, 20));	
			}
			{
				TextView t = new TextView(mContext);
				String strTitle = location.getString( location.getColumnIndex(DbLocations.KEY_TITLE) );
				t.setText( strTitle );

				ll.addView(t, new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, 24));
			}
            
			return ll;
		}
    }
}
